subroutine read_ML_catalog(c,nd)

! si legge qui l'uscita della seguente query al db seisev:
!
!SELECT
!    *
!FROM
!    seisev.hypocenter_h2 
!LEFT JOIN
!    seisev.phase_h2
!ON  seisev.hypocenter_h2.id=seisev.phase_h2.fk_hypocenter_h2
!WHERE
!"    seisev.hypocenter_h2.depth < ""40"""
!AND
!"    seisev.hypocenter_h2.mag > ""1.9"""
!AND
!"    seisev.hypocenter_h2.mag_type=""ml"""
!AND
!"    seisev.hypocenter_h2.ot > ""2005-04-16 00:00:00"""
!AND
!"    ""2012-05-01 00:00:00"" > seisev.hypocenter_h2.ot"
!id,ot,usec,lat,lon,depth,fix_depth,nph,nph_s,nph_tot,nph_fm,quality,
!azim_gap,min_distance,rms,e0_az,e0_dip,e0,e1_az,e1_dip,e1,e2,err_h,err_z,
!mag,mag_type,mag_err,mag_nsta,mag_quality,region,
!version,fk_event,fk_hypocenter,writer,creator,file_server,
!id_locator,web_id_locator,picking,modified,
!
!id,net,sta,cha,loc,cha_code,phase_code,isc_code,a_t,usec,firstmotion,emersio,takeoff,
!pamp,w,loc_w,res,dist,azim,dur,dur_mag,dur_mag_w,dur_mag_used,
!amp1_dt,amp1_usec,amp1,amp1_period,amp2_dt,amp2_usec,amp2,amp2_period,
!amp_mag,amp_mag_type,amp_type,amp_mag_used,writer,fk_phase,fk_scnl,fk_hypocenter_h2,
!modified
!

    USE PARSE_CSV
    include 'seisev_EVENT_PAHSE_structure.FI'
    character*80 fil
    character*1024 line
    logical success
    integer istart,iend,len,ich,i1,i2,add,i,ier,nd
    character*1 sep
    data sep/'$'/

    write(*,*)' subroutine: read_ML_catalog'
    is=0
! open seisev catalog (query join hypocenter_h2 with phase_h2)
    success=systemqq(' dir  ')
    1 write(*,*)' please insert a seisev hypocenterh2_phaseh2 file name:'
    read(*,'(a)',err=1)fil
    
! fil='sorted_prova.csv'   !!!
    open(status='old',readonly,unit=2,file=fil,err=99)
    open(status='unknown',unit=22,file='copy_of_used_data.txt')
    n=0
    nd=0
    2 read(2,'(q,a)',end=9)len,line
    
! count the read records
    n=n+1  
    i=mod(n,1000)
    if(i.eq.0 .and. nd.gt.0)write(*,*)' n ',n,' nd ',nd
    if(line(1:1).eq.'#')then
! it is only a comment
       goto 2
    else    
!  READ AN HYPOCENTER
       nd=nd+1
       i1=1

       call parse_csv_var(sep,ier,line,i1,i2,len,c(nd).id_hyp)    
       call parse_csv_var(sep,ier,line,i1,i2,len,c(nd).ot        ,ich)    
       call parse_csv_var(sep,ier,line,i1,i2,len,c(nd).usec)    
       call parse_csv_var(sep,ier,line,i1,i2,len,c(nd).lat)    
       call parse_csv_var(sep,ier,line,i1,i2,len,c(nd).lon)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).depth  )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).fix_depth     ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,  c(nd).nph   )    
       call parse_csv_var(sep,ier,line,i1,i2,len,  c(nd).nph_s   )    
       call parse_csv_var(sep,ier,line,i1,i2,len,  c(nd).nph_tot   )    
       call parse_csv_var(sep,ier,line,i1,i2,len,  c(nd).nph_fm   )    
       call parse_csv_var(sep,ier,line,i1,i2,len,  c(nd).quality      ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).azim_gap  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).min_distance  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).rms  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).e0_az  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).e0_dip  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).e0  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).e1_az  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).e1_dip  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).e1  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).e2  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).err_h  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).err_z  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).mag  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).mag_type     ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,c(nd).mag_err  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,c(nd).mag_nsta )    
       call parse_csv_var(sep,ier,line,i1,i2,len,c(nd).mag_quality ,ich)    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).region     ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).dpc_time     ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).type_event     ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).version  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).fk_event  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).fk_hypocenter  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).writer     ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).creator     ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).file_server ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).id_locator  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).web_id_locator  )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).picking   )    
       call parse_csv_var(sep,ier,line,i1,i2,len,   c(nd).modified     ,ich )    
 
 
 ! READ A PHASE:
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).id_ph)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).net,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).sta,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).cha,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).loc,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).cha_code,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).phase_code,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).isc_code,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).a_t,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).usec_ph)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).firstmotion,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).emersio,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).takeoff)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).pamp)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).w)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).loc_w)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).res)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).dist)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).azim)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).dur)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).dur_mag)   
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).dur_mag_w)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).dur_mag_used,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp1_dt,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp1_usec)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp1)
       c(nd).WAok = .FALSE.   ! suppose error
       if(   ABS(c(nd).amp1) .eq. 0.0 .or. ier .gt.0)then
            nd = nd-1
            goto 2     ! eliminate this data. Go to the next.
       endif
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp1_period)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp2_dt   ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp2_usec    )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp2    )
       if(   ABS(c(nd).amp2) .eq. 0.0 .or. ier .gt.0)then
            nd = nd-1
            goto 2     ! eliminate this data. Go to the next.
       else
            c(nd).WA_pha_amp=ABS(c(nd).amp2-c(nd).amp1)/2. ! Wood-Anderson amplitude
            c(nd).WAok = .TRUE.   ! abbiamo una ampiezza WA ok
       endif
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp2_period    )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp_mag    )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp_mag_type   ,ich ) 
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp_type    )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp_mag_used   ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).writer_ph   ,ich )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).fk_phase    )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).fk_scnl    )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).fk_hypocenter_h2    )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).modified_ph   ,ich ) 

! STATION (seisnet) SECTION
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_id)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_id_inter   ,ich )  
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_id_loc     ,ich )  
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_name       ,ich )  
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_start_time ,ich )  
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_end_time   ,ich )  
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_latitude)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_longitude) 
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_altitude)  
                   
! COMPUTED AT THE QUERY...
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_h_epidist) 
! ...AND NOW
       c(nd).s_h_hypdist =                              &
             SQRT(c(nd).s_h_epidist*c(nd).s_h_epidist + &
                  c(nd).depth*c(nd).depth)
    endif
    write(22,'(a)')line(1:len)
    goto 2

9   write(*,*)' I have read ',n,' records'
    write(*,*)' and ',nd,' good phases'
    !pause!pause
    return

99  write(*,*)' error in reading catalog at record ',n
    pause 
    stop
return
end
! **************************************************

